class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums)
    {
        vector<vector<int>> ret;

        sort(nums.begin(), nums.end());

        int n = nums.size();
        for (int i = 0; i < n;)
        {
            if (nums[i] > 0) break;
            int left = i + 1, right = n - 1, targe = -nums[i];
            while (right > left)
            {
                if (nums[left] + nums[right] > targe)
                {
                    right--;
                }
                else if (nums[left] + nums[right] < targe)
                {
                    left++;
                }
                else
                {
                    ret.push_back({ nums[i],nums[left],nums[right] });
                    left++, right--;
                    while (right > left && nums[left] == nums[left - 1]) left++;
                    while (right > left && nums[right] == nums[right + 1]) right--;
                }
            }
            i++;
            while (i < n && nums[i - 1] == nums[i]) i++;

        }
        return ret;
    }
};